package com.lanjiu.lib.business.task;

import com.lanjiu.pro.file.FileContent;
import com.lanjiu.pro.file.OriginalPictureVideoFriendsTransmission;
import com.lanjiu.pro.file.OriginalPictureVideoGroupTransmission;
import com.lanjiu.lib.business.FileContentFactory;
import com.lanjiu.lib.business.FileTransmissionCache;
import com.lanjiu.lib.business.TransmissionConstant;
import com.lanjiu.lib.business.handler.FileReceiverHandler;
import com.lanjiu.lib.business.listener.FileTransmissionListener;
import com.lanjiu.lib.business.listener.OnTransmissionFinishListener;
import com.lanjiu.lib.business.option.FileTransmissionReceiver;
import com.lanjiu.lib.utils.file.FileOperator;
import com.lanjiu.lib.utils.log.Logger;
import com.lanjiu.lib.utils.sp.SharedPreferenceHelper;

import java.io.File;

/**
 * @description: 下载任务
 * @author: Panwei
 * @date: 2019/11/4 14:00
 */
public class DownloadTask extends TransmissionTask implements Runnable {

    private String mTargetPath;
    private String mServerPath;
    private FileReceiverHandler mFileReceiverHandler;
    private SharedPreferenceHelper mSPHelper;

    private OriginalPictureVideoFriendsTransmission mFriend;
    private OriginalPictureVideoGroupTransmission mGroup;
    private boolean isGroup;

    public static DownloadTask newTask(String targetPath, String serverPath, FileTransmissionListener.OnFileReceiverListener listener, OriginalPictureVideoFriendsTransmission friend) {
        if (contains(serverPath)) {
            return null;
        } else {
            add(serverPath);
            return new DownloadTask(targetPath, serverPath, listener, friend);
        }
    }

    public static DownloadTask newTask(String targetPath, String serverPath, FileTransmissionListener.OnFileReceiverListener listener, OriginalPictureVideoGroupTransmission group) {
        if (contains(serverPath)) {
            return null;
        } else {
            add(serverPath);
            return new DownloadTask(targetPath, serverPath, listener, group);
        }
    }

    private DownloadTask(String targetPath, String serverPath, FileTransmissionListener.OnFileReceiverListener listener) {
        this.mTargetPath = targetPath;
        this.mServerPath = serverPath;
        this.mFileReceiverHandler = new FileReceiverHandler(listener);
        this.mSPHelper = SharedPreferenceHelper.create(TransmissionConstant.FILE_TRANSMISSION_SP_NAME);
    }

    private DownloadTask(String targetPath, String serverPath, FileTransmissionListener.OnFileReceiverListener listener, OriginalPictureVideoFriendsTransmission friend) {
        this(targetPath, serverPath, listener);
        this.mFriend = friend;
        this.isGroup = false;
    }

    private DownloadTask(String targetPath, String serverPath, FileTransmissionListener.OnFileReceiverListener listener, OriginalPictureVideoGroupTransmission group) {
        this(targetPath, serverPath, listener);
        this.mGroup = group;
        this.isGroup = true;
    }

    @Override
    public void run() {
        File targetFile = new File(mTargetPath);

        FileContent mFileContent;
        FileTransmissionCache mFTCache;
        if (!targetFile.exists()) {
            // 下载
            mFTCache = new FileTransmissionCache(mServerPath, mTargetPath);
            mFileContent = FileContentFactory.createDownloadFileContent(mServerPath);
        } else {
            if (mSPHelper.contains(mTargetPath)) {
                // 断点下载
                mFTCache = mSPHelper.getObject(mTargetPath, FileTransmissionCache.class);
                if (mFTCache != null && mFTCache.getTotalLength() == targetFile.length()) {
                    Logger.print("断点下载" + mFTCache);
                    mFileContent = FileContentFactory.createDownloadFileContent(mFTCache);
                    if (mFileReceiverHandler != null)
                        mFileReceiverHandler.sendStartMessage();
                } else {
                    mFTCache = new FileTransmissionCache(mServerPath, mTargetPath);
                    mFileContent = FileContentFactory.createDownloadFileContent(mServerPath);
                }
            } else {
                // 重新下载
                FileOperator.delete(targetFile);
                mFTCache = new FileTransmissionCache(mServerPath, mTargetPath);
                mFileContent = FileContentFactory.createDownloadFileContent(mServerPath);
            }
        }
        FileTransmissionReceiver receiver = new FileTransmissionReceiver(mSPHelper, mFileReceiverHandler);
        receiver.setOnTransmissionFinishListener(getTransmissionFinishListener());

        if (isGroup)
            receiver.transmissionFileGroupReceive(mFTCache, mFileContent, mGroup);
        else
            receiver.transmissionFilePersonReceive(mFTCache, mFileContent, mFriend);
    }

    @Override
    protected OnTransmissionFinishListener getTransmissionFinishListener() {
        return TransmissionTask::remove;
    }
}
